<?php
class FacebookFeedsController extends FacebookAppController {
	var $name = 'FacebookFeeds';
	var $helpers = array('Html', 'Form');
	
	// Model
	var $uses = array('FacebookFeedsController',FACEBOOK_DEFAULT_PRODUCT);
	// Components
	var $components = array('FacebookActionRecorder');

	/** 
	 * Name: beforeFilter 
	 * Desc: Make sure users are logged into facebook 
	 */  
	function beforeFilter() {  
		$this->user = $this->facebook->require_login();  
	} 

	/**
	 * Show the form to invite friends to join this app
	 */
	function index() {
//		Configure::write('debug', '0');
		
		$this->set('selected_menu', "invites");
	}
	
	function multi_feed_handler() {
		$this->set('user', $this->user);
		$canvas_url = FACEBOOK_CANVAS_URL;
		
		$model_name = $this->params['pass'][0];
		$object_id = $this->params['pass'][1];		
		if(class_exists($model_name)) {
			$this->set('product_model_name', $model_name);
			$product = new $model_name;
			$instance = $product->findById($object_id);
			$this->set('product', $instance);
			
			// Return URL
			$next_url = FACEBOOK_CANVAS_URL.'facebook_catalogs/details/'.$model_name.'/'.$instance[$model_name]['id'];
			
			// Image for the feed
            $images = array(array('src'=>$instance[$model_name]['thumbnail_image_src'],
								  'href' => $next_url)
							);
			
            $feed = array('template_id'=>'65315739021',
            			  'template_data'=> array('cardTitle'=>$instance[$model_name]['title'],
                                         		  'images' => $images)
						  );
			
		
            $data = array ('method'=>'multiFeedStory',
            	'content'=> array ('feed'=>$feed,
            	'next'=>$next_url
            ));
			
			// Record the action
			$sent_to = array();
			foreach($this->params[form][ids] as $friend) {
				array_push($sent_to, $friend);	
			}
			$this->FacebookActionRecorder->recordSentProduct(&$this->facebook_actions, $model_name, $this->user, $sent_to, $object_id, 'sent');
			
		} else {
            $data = array ('errorCode'=>'2',
            'errorTitle'=>'No product selected',
            'errorMessage'=>'There was a problem processing this request.  We apologize.');
        }
		
		$this->set('data', json_encode($data));
		
		$this->render('feed_sent','ajax');
	}


	/**
	 * This page is called after an invite is sent to a user's friends
	 * @return 
	 */
	function sent() {
		Configure::write('debug', '0');

	}
	
}
?>
